package CommonGui;

import javax.swing.JOptionPane;

import jpos.CashDrawer;
import jpos.JposException;
import jpos.POSPrinter;
import jpos.POSPrinterConst;

public  class ManagePrinter {
	
	private POSPrinter jPos_Printer;
	private CashDrawer cashDrawer;
	private int nCut;
	
	
	public ManagePrinter()
	{
		InitDialog();
	}
	
	
	private void InitDialog() {
		boolean bError = false;
	//	System.out.println("BEGIN");
		jPos_Printer = new POSPrinter();
		
		while (true) {
			try {
				jPos_Printer.open("byprn");
			} catch (JposException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,
						"This device has not been registered, or can't use.",
						"Printer", 0);
				bError = true;
				break;
			}

			try {
				jPos_Printer.claim(1);
			} catch (JposException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,
						"Fails to get the exclusive right for the device.",
						"Printer", 0);
				bError = true;
				break;
			}

			try {
				jPos_Printer.setDeviceEnabled(true);
			} catch (JposException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,
						"Now the device is disabled to use.", "Printer", 0);
				bError = true;
				break;
			}
			System.out.println("MapMode");
			try {
				jPos_Printer.setMapMode(POSPrinterConst.PTR_MM_METRIC);
			} catch (JposException e) {
				e.printStackTrace();
			}
			System.out.println("MapMode1");
			/*try {
				int iRecLineWidth = jPos_Printer.getRecLineWidth();
				jPos_Printer.setBitmap(1, POSPrinterConst.PTR_S_RECEIPT,
						"bmp\\Lion.bmp", iRecLineWidth / 3,
						POSPrinterConst.PTR_BM_CENTER);
			} catch (JposException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null,
						"Can not find the specific bitmap", "Printer", 0);
			}*/
			break;
		}
		
		
		 cashDrawer = new CashDrawer();
		try {
			cashDrawer.open("cashCool");
			cashDrawer.claim(10000);
			cashDrawer.setDeviceEnabled(true);
		} catch (JposException e) {
			JOptionPane.showMessageDialog(null,
					"The device has not been registered,or can't use.");
			
		}
				
		nCut = 0;
	}


	public POSPrinter getjPos_Printer() {
		return jPos_Printer;
	}


	public void setjPos_Printer(POSPrinter jPos_Printer) {
		this.jPos_Printer = jPos_Printer;
	}


	public CashDrawer getCashDrawer() {
		return cashDrawer;
	}


	public void setCashDrawer(CashDrawer cashDrawer) {
		this.cashDrawer = cashDrawer;
	}


	public int getnCut() {
		return nCut;
	}


	public void setnCut(int nCut) {
		this.nCut = nCut;
	}
	
	

}
